package com.ts.crawler.domain;

import com.ts.crawler.common.CrawlerInfo;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.JsonPathSelector;

public class SearchIdProcesser implements PageProcessor {

    private static String firstUrl = "http://flights.ctrip.com/";

    private Site site = Site.me().setSleepTime(1000).setTimeOut(10000).setCharset("utf-8")
            .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");

    public void process(Page page) {
        page.putField("searchId",new JsonPathSelector("$.data.context.searchId").select(page.getRawText()));
        //{status=0, msg=success, data={context={searchId=j1047165116-1633838210774-1595333lr-0, finished=false, flag=1}, bestChoiceFlightsForceTop=false}}
        //[{"status":0,"msg":"success","data":{"context":{"searchId":"j1058191123-1633838381901-1677261AT-0","finished":false,"flag":1},"bestChoiceFlightsForceTop":false}}]
        ResultItems resultItems = page.getResultItems();
        String searchId = resultItems.get("searchId");
        CrawlerInfo.searchId=searchId;
    }

    public Site getSite() {
        return site;
    }
}
